<?php
/**
 * $Id: lang.class.php 438 2006-07-21 13:14:04Z tito $
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

class fgsLang {

	function init() {
		fgsLang::set($_SESSION['lang']);
	}

	function getList() {
		return array(
			'fr' => tr('Francais'),
			'en' => tr('Anglais'),
		);
	}

	function set($lang = 'en') {
		if ($lang != '' && $_SESSION['lang'] != $lang)
			$_SESSION['lang'] = $lang;
		else if ($_SESSION['lang'] == '') {
			$langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
			if (isset($langs[0]))
				$lang = substr($langs[0], 0, 2);
			else
				$lang = 'en';
				
			$_SESSION['lang'] = $lang;
		}
		else
			$lang = $_SESSION['lang'];

		$_SESSION['lang'] = $lang;
		fgs::config('lang', $lang);

		$filename = sprintf('scripts/lang/%s.php', $lang);
		if (file_exists($filename)) {
			$data = include($filename);
			fgs::config('langdata', $data);
		}
	}

	function getLang() {
		return fgs::config('lang');
	}
	
	function tr($str) {
		$lang = fgs::config('lang');
		$langdata = fgs::config('langdata');
		if ($lang == '' || !is_array($langdata))
			return $str;
		elseif (!array_key_exists($str, $langdata)) {
			if ($lang != 'en')
				return '[[' . $str . ']]';
			else
				return $str;
		} else
			return $langdata[$str];
	}

}

function tr($str) {
	return fgsLang::tr($str);
}

?>
